package com.dongdongshop.shiroconfig;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class cofig {

    //将ShiroFilterFactoryBean放入容器
    @Bean
    public ShiroFilterFactoryBean getshiroFilterFactoryBean(@Autowired DefaultWebSecurityManager defaultWebSecurityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        //设置安全管理器
        shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager);
        //配置Shiro内置过滤器
        /*
         * anon:无需认证就可以访问
         * authc:必须认证才可以访问
         * perms:权限
         * role:登录之后要有角色才能使用
         * logout:退出登录
         * */
        Map<String, String> map = new LinkedHashMap<>();
        //放开跳转登录请求
//        map.put("/skip/tologin", "anon");
        //放开登陆验证请求
//        map.put("/TbAdmin/login", "anon");
        //放开静态页面
//        map.put("/css/**", "anon");
//        map.put("/img/**", "anon");
//        map.put("/js/**", "anon");
//        map.put("/plugins/**", "anon");
        //退出登录请求拦截执行
        map.put("/logout", "logout");
        //拦截其他数据
//        map.put("/**", "authc");
        map.put("/skip/tohomeindex", "authc");
        map.put("/skip/togetOrderInfo", "authc");
        //放入方法中实现
        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
        //修改调整登陆页面
        shiroFilterFactoryBean.setLoginUrl("/skip/tologin");
        //权限不足跳转页面
//        shiroFilterFactoryBean.setUnauthorizedUrl("/loginController/UnauthorizedUrl");
        return shiroFilterFactoryBean;
    }

    //将DefaultWebSecurityManager放入容器
    @Bean
    public DefaultWebSecurityManager getdefaultWebSecurityManager(@Autowired Realm Realm) {
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        //关联Realm
        defaultWebSecurityManager.setRealm(Realm);
        return defaultWebSecurityManager;
    }

    //将Realm放入容器
    @Bean
    public Realm getrealm(@Autowired HashedCredentialsMatcher credentialsMatcher) {
        Realm realm = new Realm();
        realm.setCredentialsMatcher(credentialsMatcher);
        return realm;
    }

    //配置加密
    @Bean
    public HashedCredentialsMatcher hashedCredentialsMatcher(){
        HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher();
        //指定加密方式
        credentialsMatcher.setHashAlgorithmName("MD5");
        System.out.println("加密方式-MD5");
        //加密次数
        credentialsMatcher.setHashIterations(3);
        //true使用hex编码,false使用base64编码
        credentialsMatcher.setStoredCredentialsHexEncoded(true);
        return credentialsMatcher;
    }

    /*
    * 配置ShiroDialect,用于thymeleaf和shiro标签的配合使用
    * */
    @Bean
    public ShiroDialect getdialect(){
        return new ShiroDialect();
    }
}
